package com.ita.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.ita.dao.AssessDao;
import com.ita.pojo.Assess;

@Repository(value = "assessDao")
public class AssessDaoImpl implements AssessDao {

	@PersistenceContext(name = "unit")
	private EntityManager manager;

	@Override
	public Assess addAssess(Assess assess) {

		manager.persist(assess);
		return assess;
	}

	@Override
	public Assess updateAssess(String id, String reply, long reply_time) {

		Assess assess = null;
		// update MUser m set m.block=:block where m.id=:id
		try {
			assess = manager.find(Assess.class, id);
			assess.setReply(reply);
			assess.setReply_time(reply_time);
			manager.flush();
		} catch (Exception e) {
			e.printStackTrace();
			return assess;
		}
		return assess;

	}

	@Override
	public List<Assess> findAll(String mid) {
		// TODO Auto-generated method stub

		String pql = "select ass from Assess ass where ass.mUser.id=:m_id";
		List<Assess> list = manager.createQuery(pql).setParameter("m_id", mid).getResultList();

		return list;
	}

	@Override
	public List<Assess> findAllByPage(String mid, int max, int begin) {
		// TODO Auto-generated method stub
		// String sql="SELECT * FROM (SELECT ROWNUM AS rowno, t.* FROM ASSESS t
		// WHERE t.m_id=:mid and ROWNUM <= :n) table_alias WHERE
		// table_alias.rowno >= :m";
		String pql = "select ass from Assess ass where ass.mUser.id=:mid";
		List<Assess> list = manager.createQuery(pql).setParameter("mid", mid).setMaxResults(max).setFirstResult(begin)
				.getResultList();
		return list;
	}

	@Override
	public List<Assess> findAllNoReplyByPage(String mid, int max, int begin) {
		// TODO Auto-generated method stub
//		String pql = "select ass from Assess ass where ass.mUser.id=:mid and ass.reply is null";
		String pql = "select ass from Assess ass where ass.mUser.id=:mid and ass.reply is null";
		List<Assess> list = manager.createQuery(pql).setParameter("mid", mid)
				.getResultList();
		return list;
	}

	@Override
	public List<Assess> findAllReplyByPage(String mid, int max, int begin) {
		// TODO Auto-generated method stub
		String pql = "select ass from Assess ass where ass.mUser.id=:mid and ass.reply is not null";
//		String pql = "select ass from Assess ass where ass.mUser.id=:mid and ass.reply is not null";
		List<Assess> list = manager.createQuery(pql).setParameter("mid", mid)
				.getResultList();
		return list;
	}

	@Override
	public int getReplySize(String mid) {
		
		String pql = "select ass from Assess ass where ass.mUser.id=:mid and ass.reply is not null";
		List<Assess> list =manager.createQuery(pql).setParameter("mid", mid).getResultList();
		return list.size();
	}

	@Override
	public int getNotReplySize(String mid) {
		// TODO Auto-generated method stub
		String pql = "select ass from Assess ass where ass.mUser.id=:mid and ass.reply is null";
		List<Assess> list =manager.createQuery(pql).setParameter("mid", mid).getResultList();
		return list.size();
	}

}
